package sk.stuba.fiit.clientApp.service.bussiness;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;

import org.springframework.beans.factory.annotation.Autowired;

import sk.stuba.fiit.clientApp.cvo.BasicPanelDataCVO;
import sk.stuba.fiit.clientApp.service.StorageService;
import sk.stuba.fiit.clientApp.service.impl.StorageServiceImpl;


public class KeyInfoBussinesData {
	@Autowired
	private StorageService storageService = new StorageServiceImpl();
	
	private X509Certificate signCertificate;

	public X509Certificate getSignCertificate() {
		return signCertificate;
	}

	public void setSignCertificate(BasicPanelDataCVO data) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, UnrecoverableEntryException, IOException, InvalidKeySpecException {
		String archiveType = data.getKeyFormat();
		String certificatePassword = (data.getKeyPassword().equals("")) ? null: data.getKeyPassword();
		X509Certificate certificate = null;
		if(archiveType.toLowerCase().equals("jks") || archiveType.toLowerCase().equals("pkcs12") ||archiveType.toLowerCase().equals("pkcs8")){
			certificate = storageService.loadCertificate(data.getCertificatePath(), certificatePassword, archiveType);
		}else{
			certificate = storageService.loadCertificateFromCertFile(data.getCertificatePath());
		}
		this.signCertificate = certificate;
	}
	
	
}
